﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

/// <summary>
/// 
/// </summary>
namespace ProgNetComponents.Validation.Rules
{
    /// <summary>
    /// Rule for checking columns length
    /// </summary>
    public class ColumnStringLengthValidationRule : ColumnValidationRule
    {
        #region Static
        /// <summary>
        /// Initializes the <see cref="ColumnStringLengthValidationRule"/> class.
        /// </summary>
        static ColumnStringLengthValidationRule()
        {
            GlobalDefaultErrorString = "Takst powinien mieć maksymalnie {0} znaków.";
        }
        /// <summary>
        /// Gets or sets the global default error string.
        /// </summary>
        /// <value>
        /// The global default error string.
        /// </value>
        public static string GlobalDefaultErrorString { get; set; }
        #endregion

        /// <summary>
        /// Gets the default error string.
        /// </summary>
        /// <value>
        /// The default error string.
        /// </value>
        protected override string DefaultErrorString
        {
            get
            {
                return GlobalDefaultErrorString;
            }
        }

        /// <summary>
        /// Gets or sets maximum allowed length
        /// </summary>
        /// <value>
        /// The length.
        /// </value>
        public int Length
        {
            get;
            set;
        }

        /// <summary>
        /// Gets formatted error string
        /// </summary>
        /// <value>
        /// The formated error string.
        /// </value>
        public override string FormatedErrorString
        {
            get
            {
                return ErrorString.XFormat(Length);
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ColumnStringLengthValidationRule"/> class.
        /// </summary>
        public ColumnStringLengthValidationRule() { }
        /// <summary>
        /// Initializes a new instance of the <see cref="ColumnStringLengthValidationRule"/> class.
        /// </summary>
        /// <param name="column">The column.</param>
        /// <param name="length">The length.</param>
        /// <param name="error">The error.</param>
        public ColumnStringLengthValidationRule(string column, int length, string error = null)
            : base(column, error)
        {
            Length = length;
        }

        /// <summary>
        /// Does the validate.
        /// </summary>
        /// <param name="row">The row.</param>
        /// <returns></returns>
        protected override bool DoValidate(DataRow row)
        {
            int val = row[Column].AsString().Length;
            return val <= Length;
        }
    }
    
}
